package projectmanagement.models;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Iteration class provides the basic model of iteration,
 * bounded by start and finish dates and containing list of tasks.
 *
 * @author Dmitry Shvets
 */
public class Iteration {

    /**
     * Duration in working days, used to count ideal burndown
     */
    private int duration = 0;
    private int id;

    private Calendar startDate;
    private Calendar finishDate;
    private String description = "N/A";
    private List<Task> tasks = new LinkedList<Task>();

    public Iteration(Calendar startDate, Calendar finishDate, int duration, String description) {
        this.startDate = startDate;
        this.finishDate = finishDate;
        this.duration = duration;
        this.description = description;
    }

    public Iteration(int duration, String description){
        this.duration = duration;
        this.description = description;
    }

    public int getDuration() {
        return duration;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public List<Task> getTasks() {
        return new LinkedList<Task>(tasks);
    }

    public Task getTaskById(int id) {
        for (Task task : tasks) {
            if (task.getId() == id)
                return task;
        }
        return null;
    }

    public void addTask(Task task) {
        tasks.add(task);
    }

    @Override
    public String toString() {
        StringBuilder result = new StringBuilder();
        result.append("Iteration ID: ").append(id).append("\n");
        result.append(tasks.size()).append(" task(s)");
        result.append(" for ").append(duration).append(" working days.\n");
        result.append("Description: ").append(description).append("\n");
        return result.toString();
    }
}
